package 
{
import core.module.IGameModule;
import core.module.ModuleManager;

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.errors.IllegalOperationError;

import framework.core.ComponentBuilder;

import modules.interfaces.ISectionModule;

import view.hero.HeroUI;
import view.interfaces.IHeroUI;
import view.interfaces.IInventoryUI;
import view.interfaces.ISectionUI;
import view.interfaces.IShopUI;
import view.interfaces.ISkillUI;
import view.inventory.InventoryUI;
import view.shop.ShopUI;
import view.skill.SkillUI;

/**
 * GameUI
 * @author jliang
 * 
 */
public class Game extends Sprite implements IGameFacade
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function Game(enforce:SingletonEnforce)
	{
		super();
		if (!enforce)
		{
			throw new IllegalOperationError("Game is a singleton class.");
		}
	}
    //==========================================================================
    //  Class constants
    //==========================================================================
	private static var _instance:IGameFacade;

	public static function get instance():IGameFacade
	{
		if (!_instance) _instance = new Game(new SingletonEnforce());
		return _instance;
	}
    //==========================================================================
    //  Properties
    //==========================================================================
    public function get container():DisplayObjectContainer
    {
        return parent;
    }
    
    private var _sectionLayer:DisplayObjectContainer;
    
    public function get sectionLayer():DisplayObjectContainer
    {
        return _sectionLayer;
    }
	
	private var _mapLayer:DisplayObjectContainer;
	
	public function get mapLayer():DisplayObjectContainer
	{
		return _mapLayer;
	}
    
    private var _popUpLayer:DisplayObjectContainer;
    
    public function get popUpLayer():DisplayObjectContainer
    {
        return _popUpLayer;
    }
    
    protected var _currentSectionUI:ISectionUI;
    
    public function get currentSectionUI():ISectionUI
    {
        return _currentSectionUI;
    }
    
    protected var _shopUI:IShopUI;
    
    public function get shopUI():IShopUI
    {
        return _shopUI;
    }    
    
    protected var _inventoryUI:IInventoryUI;
    
    public function get inventoryUI():IInventoryUI
    {
        return _inventoryUI;
    }
	
	protected var _skillUI:ISkillUI;
	
	public function get skillUI():ISkillUI
	{
		return _skillUI;
	}
	
	protected var _heroUI:IHeroUI;
	
	public function get heroUI():IHeroUI
	{
		return _heroUI;
	}
    //==========================================================================
    //  Public methods
    //==========================================================================	
	public function initialize(con:DisplayObjectContainer, params:Object):void
	{                
        con.addChild(this);
        
        //init layers
        _sectionLayer = new Sprite();
        sectionLayer.name = "sectionLayer";
        this.addChild(sectionLayer);
		
		_mapLayer = new Sprite();
		mapLayer.name = "mapLayer";
		this.addChild(mapLayer);
        
        _popUpLayer = new Sprite();
        popUpLayer.name = "popUpLayer";
        this.addChild(popUpLayer);
        
        //start();
        
        //check new player
        if (params.isNewPlayer == true)
        {
            ModuleManager.load("create_player", createPlayerReadyHandler);
        }
        else
        {
            start();
        }
	}
    
    public function start():void
    {
        //init shop & inventory
        if (!shopUI)
        {
            _shopUI = ComponentBuilder.create(ShopUI);
        }
        
        if (!inventoryUI)
        {
            _inventoryUI = ComponentBuilder.create(InventoryUI);
        }
		
		if(!skillUI)
		{
			_skillUI = ComponentBuilder.create(SkillUI);
		}
		
		if(!heroUI)
		{
			_heroUI = ComponentBuilder.create(HeroUI);
		}
        
        //init first section
		changeSection("city_section");
    }
	
	public function changeSection(...args):void
	{
		var moduleKey:String = args[0];
		ModuleManager.load(moduleKey, sectionReadyHandler);
	}
    //==========================================================================
    //  Event handlers
    //==========================================================================
    protected function createPlayerReadyHandler(module:IGameModule):void
    {
        sectionLayer.addChild(module as DisplayObject);  
    }
    
    protected function sectionReadyHandler(module:ISectionModule):void
    {
		if (currentSectionUI)
		{
			if (currentSectionUI.displayObject && currentSectionUI.displayObject.parent)
			{
				currentSectionUI.displayObject.parent.removeChild(currentSectionUI.displayObject);
			}
			currentSectionUI.dispose();
			_currentSectionUI = null;
		}
        _currentSectionUI = module;
        
        sectionLayer.addChild(module.displayObject);        
    }
}
}

class SingletonEnforce
{
	
}


